package com.util;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

/**
 * <p>
 *
 * @author: yuanwl
 * @date: 2020/12/31 14:13
 */
public class TaskUtil {

    private static final int THREAD_TOTAL = 20;

    public static void runMultiTasks(int taskCount, String dstDirPath, Consumer<String> taskCallback) {
        CountDownLatch countDownLatch = new CountDownLatch(taskCount);
        ExecutorService es = Executors.newFixedThreadPool(THREAD_TOTAL);
        for (int i = 0; i < taskCount; i++) {
            es.execute(() -> taskCallback.accept(dstDirPath));
            countDownLatch.countDown();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        es.shutdown();
    }
}
